/*
 * @Author: Zhouhai 497675647@qq.com
 * @Date: 2022-12-01 14:14:47
 * @LastEditors: Zhouhai 497675647@qq.com
 * @LastEditTime: 2022-12-01 17:48:25
 */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { getRoleCorpInfoByUserId, updateUserProfile, updateUserPwd } from '@/api/system/user';
import { useStore } from '@/store';
import { CorpDomain } from '@/type.d/system';
import { Button, Form, Input, message, Radio, Select } from 'antd';
import React, { useEffect, useState } from 'react'
import './index.less';
const UserInfo: React.FC = () => {
    const { userStore } = useStore()
    const [form] = Form.useForm();
    const { layoutStore } = useStore()

    const [corpOption,setCorpOtion] = useState<any[]>([])


    useEffect(()=>{
        if(userStore.user.userId){
            layoutStore.OpenSpanning()
            getRoleCorpInfoByUserId(userStore.user.userId).then((res)=>{
                setCorpOtion(res.data?.map((item: CorpDomain)=>{
                    return {
                        label: item.corpName,
                        value: item.pkCorp
                    }
                }))
            }).finally(()=>{
                layoutStore.CloseSpanning()
            })
        }
        
    },[])

    const submit = (data: any) => {
        layoutStore.OpenSpanning()
        console.log('data', data);

        updateUserProfile({ ...userStore.user, ...data }).then((res) => {
            userStore.setUser(res.data)
            // form.resetFields()
            message.success('修改成功！')
        }).finally(() => layoutStore.CloseSpanning())

    }
    return (
        <Form
            initialValues={userStore.user}
            onFinish={submit}
            form={form}
            name="basic"
            autoComplete="off"
            labelCol={{ span: 2 }}
            wrapperCol={{ span: 21 }}>
            <Form.Item
                label="默认组织"
                name="corpId"
            >
                <Select options={corpOption}/>
            </Form.Item>
            <Form.Item
                label="用户昵称"
                name="nickName"
                rules={[{ required: true }]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="手机号码"
                name="phonenumber"
                rules={[{ required: true }]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="邮箱"
                name="email"
                rules={[
                    { required: true },
                ]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="性别"
                name="sex"
                rules={[
                    { required: true },
                ]}
            >
                <Radio.Group>
                    <Radio value="0">男</Radio>
                    <Radio value="1">女</Radio>
                </Radio.Group>
            </Form.Item>
            <Form.Item
                wrapperCol={{ span: 14, offset: 1 }}
            >
                <Button type="primary" htmlType="submit">提交</Button>
            </Form.Item>
        </Form >
    )

}
export default UserInfo;